
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import { createRouter, createWebHashHistory } from 'vue-router'
const router = createRouter({
  history: createWebHashHistory(),
  routes:[
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect:'/home/orders',
        children:[
            {
                 path: 'users',
                 component: ()=>import('./components/subcomponents/MyUsers.vue')
            },
            {
                   path: ':id',
                    name: 'details',
                    component: () => import('./components/user/MyUserDetail.vue'),props: true
            },
            {
                path:'orders',
                component:()=>import('./components/subcomponents/MyOrders.vue')
            },
            {
                path:'menu',
                component:()=>import('./components/subcomponents/MyMenu.vue')
            }
        ]
    }
  ]
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        return next()
    }
    const token = localStorage.getItem('token')
    if (!token) {
        return next('/login')
    }
    next()
})
export default router;
